package com.neasho.neashoadminv1.infrastructure.domain.base

import org.babyfish.jimmer.kt.isLoaded
import org.babyfish.jimmer.sql.DraftInterceptor
import org.springframework.stereotype.Component
import java.time.LocalDateTime

/**
 * bean属性自动填充值
 */
@Component
class BaseEntityDraftInterceptor(
) : DraftInterceptor<BasePermissionEntity, BasePermissionEntityDraft> {

    override fun beforeSave(draft: BasePermissionEntityDraft, original: BasePermissionEntity?) {
        if (!isLoaded(draft, BaseEntity::lastModifyAt)) {
            draft.lastModifyAt = LocalDateTime.now()
            draft.lastModifyBy {
                id = 1
            }
        }

        if (original === null) {
            if (!isLoaded(draft, BaseEntity::createAt)) {
                draft.createAt = LocalDateTime.now()
                draft.createBy {
                    id = 1
                }
            }
        }
    }
}
